﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NGCDS
{
    public abstract class Protocol
    {
        protected const int BUFFER_SIZE = 4096;
        protected string[] ZIPs = { ".gz", ".bz2" };
        protected NetworkExtension netEx = null;
        protected LogHandeler log = null;
        protected Session session = null;

        public abstract string getAuthenticationID(bool isAdminUser = false);
        public abstract SortedDictionary<string, RemoteFileInfo> GetFileList();
        public abstract bool DownloadFile(RemoteFileInfo remoteFileInfo, string localFile);

        public bool isLocalFileExist(RemoteFileInfo rfi)
        {
            // 一般情況會檔案會在 finish，因此先找 finish 目錄節省時間
            // finish
            if (session.getLocalSrcPath() != null)
            {
                string localSrcFile = session.getLocalSrcPath() + rfi.FileName;
                if (File.Exists(localSrcFile))
                {
                    FileInfo fi = new FileInfo(localSrcFile);
                    if (fi.Length == rfi.Length)
                    {
                        return true;
                    }
                }

                foreach (string zip in ZIPs)
                {
                    if (File.Exists(localSrcFile + zip))
                    {
                        return true;
                    }
                }
            }

            // local
            string localFile = session.getLocalPath() + rfi.FileName;
            if (File.Exists(localFile))
            {
                FileInfo fi = new FileInfo(localFile);
                if (fi.Length == rfi.Length)
                {
                    return true;
                }
            }

            foreach (string zip in ZIPs)
            {
                if (File.Exists(localFile + zip))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
